# -*- coding: utf-8 -*-
'''
Created on 2017年5月17日

@author: ZhuJiahui
'''

import os
import time
import requests
from file_utils.file_writer import write_1d_to_text

def get_avp(primary_entity, entity):
    
    avp_triples = []
    avp_prefix = 'http://knowledgeworks.cn:20313/cndbpedia/api/entityAVP?entity='
    avp_request = requests.get(avp_prefix + primary_entity)
    if (avp_request.status_code == 200):
        avp_text = avp_request.json()
        attribute_list = avp_text['av pair']
        if (len(attribute_list) > 0):
            for each_attribute in attribute_list:
                avp_triples.append([entity, each_attribute[0], each_attribute[1].replace('\n', ' ')])
    
    return avp_triples


def cndbpedia_get_triples(entity):
    
    mention2entity_prefix = 'http://knowledgeworks.cn:30001/?p='
    primary_prefix = 'http://knowledgeworks.cn:30001/?inpage=1&p='
    
    primary_request = requests.get(primary_prefix + entity)
    if (primary_request.status_code == 200):
        primary_text = primary_request.text
        star_index = primary_text.find('*primary')
        if (star_index > -1):

            start_index = star_index - 1
            while primary_text[start_index] != '>':
                start_index -= 1
    
            primary_entity = primary_text[(start_index + 1):(star_index - 1)].strip()
            avp_pairs = get_avp(primary_entity, entity)
        else:
            entity_request = requests.get(mention2entity_prefix + entity)
            if (entity_request.status_code == 200):
                entity_list = entity_request.json()
                if (len(entity_list) > 0):
                    primary_entity = entity_request.json()[0]
                    avp_pairs = get_avp(primary_entity, entity)

    return avp_pairs

if __name__ == '__main__':
    start = time.clock()    
    now_directory = os.getcwd()
    root_directory = os.path.dirname(now_directory) + '/'
    read_filename = root_directory + 'dataset/sogou/train_feature_words.txt'
    write_filename = root_directory + 'dataset/sogou/train_triples.txt'
    
    selected_word_list = []
    with open(read_filename, 'r', encoding='gbk') as f:
        for each_line in f:
            selected_word_list.append(each_line.split()[0].split('||')[0])
    
    all_triples = []
    for i in range(len(selected_word_list)):
        print(i)
        avp_pairs = cndbpedia_get_triples(selected_word_list[i])
        for each in avp_pairs:
            all_triples.append("\t|\t".join(each))
    
    write_1d_to_text(write_filename, all_triples)
    print('Total time %f seconds' % (time.clock() - start))
    